{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "aca5ff5c",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "7edeaa42",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1. 2. 3.]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "numpy.ndarray"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = np.array([1.0, 2.0, 3.0])\n",
    "print(x)\n",
    "type(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "7bc69e56",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([3., 6., 9.])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y = np.array([2.0, 4.0, 6.0])\n",
    "x+y"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b4be0920",
   "metadata": {},
   "source": [
    "#### element-wise加减乘除，即“对应元素的运算”，我喜欢翻译成“逐元素运算”"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "8c8f5db4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-1., -2., -3.])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x-y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "054f41ce",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 2.,  8., 18.])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x*y"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eac968ee",
   "metadata": {},
   "source": [
    "#### 这里的运算都是x[0] ? y[0]，按行运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "eaf234a9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.5, 0.5, 0.5])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x/y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "4e33ac42",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[51, 55],\n",
       "       [14, 19],\n",
       "       [ 0,  4]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X = np.array([[51, 55], [14, 19], [0, 4]])\n",
    "X"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "ed454eac",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([51, 55, 14, 19,  0,  4])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X = X.flatten()  #一维数组转换\n",
    "X"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "afeae0b1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([51, 14,  0])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X[np.array([0, 2, 4])] # 获取索引为0、2、4的元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "8bcea689",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ True,  True, False,  True, False, False])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Y = X > 15\n",
    "Y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "59e8768f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 1, 0, 1, 0, 0])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Y = Y.astype(int)\n",
    "Y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "13431649",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([51, 55, 19])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X[X>15]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f9fcd771",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
